//获取应用实例
const app = getApp();

const common = require('./../../../utils/common.js');

Page({
    data: {
        now_page: 0,
        list_total: 0,
        supplier_list: [],
        keywords: '',
        limit: 20,
        // 菜单栏
        active_nav_index: 0,
        nav_list: [
            {name: '全部', value: 'all'},
            {name: '面料供应商', value: 'fabricSupplier'},
            {name: '辅料供应商', value: 'accessoriesSupplier'},
            {name: '耗材供应商', value: 'consumablesSupplier'},
        ],
    },

    onLoad: function (options) {
        // 设置标题
        wx.setNavigationBarTitle({
            title: '物料供应商'
        });
        // 获取列表
        this.get_suppliser_list();
    },

    // 每次显示时
    onShow() {

    },

    // 页面触底时执行
    onReachBottom: function () {
        this.get_suppliser_list();
    },

    // 监听下拉[刷新]
    onPullDownRefresh: function () {
        this.setData({
            supplier_list: [],
            list_total: 0,
            now_page: 0,
        });
        // 获取列表
        this.get_suppliser_list();
        // 停止下拉
        setTimeout(() => {
            wx.stopPullDownRefresh();
        }, 1000)
    },

    // 获取供应商列表
    get_suppliser_list: function (is_clean = 0) {
        // 验证是否超出最大页码
        if ((this.data.supplier_list.length >= this.data.list_total) && this.data.now_page > 0) {
            return false;
        }
        // 请求参数
        let req_data = {
            current: this.data.now_page + 1,
            size: this.data.limit,
            selectParameter: { keyword: this.data.keywords, tabValue: this.data.nav_list[this.data.active_nav_index].value},
        };
        // 发起请求
        const result = common.request_api('/api/pdm/mdSupplier/getMdSupplierList', req_data);
        result.then(res => {
            console.log(res)
            if (is_clean == 1) {
                this.setData({
                    supplier_list: [],
                    list_total: 0,
                    now_page: 0,
                });
            }
            this.setData({
                supplier_list: this.data.supplier_list.concat(res.data.records != null ? res.data.records : []),
                list_total: res.data.total,
                now_page: this.data.now_page + 1
            });
        })
    },

    // 搜索
    GoSearch: function (events) {
        var keywords = events.detail.value.trim();
        this.setData({
            keywords: keywords,
            supplier_list: [],
            now_page: 0,
            list_total: 0,
        });
        this.get_suppliser_list(1);
    },

    // 点击菜单栏时
    change_nav: function (events) {
        const index = events.currentTarget.dataset.index;
        this.setData({
            active_nav_index: index,
            supplier_list: [],
            now_page: 0,
            list_total: 0,
        });
        this.get_suppliser_list(1);
    },

    // 添加供应商
    supplier_add: function (events) {
        const __this = this;
        // 跳转到创建样板页面
        wx.navigateTo({
            url: '/alpha/pages/alpha_supplier_add/index',
            events: {
                // 当添加完成时重新请求数据
                changeData: function (data) {
                    __this.setData({
                        keywords: '',
                        supplier_list: [],
                        now_page: 0,
                        list_total: 0,
                    });
                    __this.get_suppliser_list();
                }
            }
        });
    },

    // 编辑供应商
    supplier_edit: function (events) {
        const index = events.currentTarget.dataset.index;
        const __this = this;
        // 跳转
        wx.navigateTo({
            url: '/alpha/pages/alpha_supplier_add/index?id=' + __this.data.supplier_list[index].id,
            events: {
                // 当修改完成时
                changeData: function (data) {
                    __this.setData({
                        keywords: '',
                        supplier_list: [],
                        now_page: 0,
                        list_total: 0,
                    });
                    __this.get_suppliser_list();
                }
            }
        });
    },

})
